﻿using System;
using Entities;
using Business;

namespace ServicesDirectory.UserControl
{
    public partial class AddProgramme : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
            lbtLookupContact.Click += LookupContact_Click;
            LookupContactManager1.CloseControl += (LookupContactManager1_CloseControl);
            LookupContactManager1.GetManager += LookupContactManager1_GetManager;
        }

        /// <summary>
        /// Handles the GetManager event of the LookupContactManager1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupContactManager1_GetManager(object sender, EventArgs e)
        {
            lbtLookupContact.CommandArgument = sender.ToString();
            string message;
            var contact = ContactBusiness.Instance.GetContact(sender.ToString(), out message);
            AddContact.Text = contact.FirstName + " " + contact.SurName;
            AddContact.Enabled = false;

        }

        /// <summary>
        /// Handles the CloseControl event of the LookupContactManager1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupContactManager1_CloseControl(object sender, EventArgs e)
        {
            pnlookupContac.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the LookupContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupContact_Click(object sender, EventArgs e)
        {
            pnlookupContac.Visible = true;

        }

        protected void Deactive_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            Programme programme = new Programme();

            programme.ProgrammeId = Guid.NewGuid();
            programme.ContactId = new Guid(lbtLookupContact.CommandArgument);
            programme.ProgrammeName = AddProName.Text;
            programme.Description = AddDes.Text;
            programme.Active = true;

            var result = ProgrammeBusiness.Instance.Insert(programme);
            if (result)
                Response.Write("<script type=\"text/javascript\">alert('Successfuly');</script>");
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListProgramme.aspx");
        }

    }
}